package com.iedu.demo.lgq.mapper;

import com.iedu.demo.lgq.entity.TbCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    // 获取所有分类（包括子分类）
    @Select("SELECT * FROM team08.tb_category WHERE status = 1 ORDER BY sort_order, name")
    List<TbCategory> getAllCategories();

    // 获取指定父类目下的所有子分类
    @Select("SELECT * FROM team08.tb_category WHERE parent_id = #{parentId} AND status = 1 ORDER BY sort_order, name")
    List<TbCategory> getSubCategoriesByParentId(@Param("parentId") Long parentId);

    // 获取指定分类
    @Select("SELECT * FROM team08.tb_category WHERE categoryId = #{categoryId} AND status = 1")
    TbCategory getCategoryById(@Param("categoryId") Long categoryId);

    // 插入一个新的分类
    @Insert("INSERT INTO team08.tb_category (parent_id, name, status, sort_order, is_parent, created, updated) " +
            "VALUES (#{parentId}, #{name}, #{status}, #{sortOrder}, #{isParent}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    int insertCategory(TbCategory category);

    // 更新分类信息
    @Update("UPDATE team08.tb_category SET parent_id = #{parentId}, name = #{name}, status = #{status}, " +
            "sort_order = #{sortOrder}, is_parent = #{isParent}, updated = NOW() WHERE categoryId = #{categoryId}")
    int updateCategory(TbCategory category);

    // 删除分类（逻辑删除）
    @Update("UPDATE team08.tb_category SET status = 2, updated = NOW() WHERE categoryId = #{categoryId}")
    int deleteCategory(@Param("categoryId") Long categoryId);
}